<template>
    <div class="page-container">
        <!--工具栏-->
        <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
            <el-form :inline="true" :model="filters" :size="size">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="用户名"></el-input>
                </el-form-item>
                <el-form-item>
                    <kt-button icon="fa fa-search" :label="$t('action.search')" perms="sys:role:view" type="primary"
                               @click="findPage(null)"/>
                </el-form-item>
                <el-form-item>
                    <kt-button icon="fa fa-plus" :label="$t('action.add')" type="primary"
                               @click="handleAdd"/>
                </el-form-item>
            </el-form>
        </div>
        <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
            <el-form :inline="true" :size="size">
                <el-form-item>
                    <el-button-group>
                        <el-tooltip content="刷新" placement="top">
                            <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="列显示" placement="top">
                            <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
                        </el-tooltip>
                    </el-button-group>
                </el-form-item>
            </el-form>
            <!--表格显示列界面-->
            <table-column-filter-dialog ref="tableColumnFilterDialog" :columns="columns"
                                        @handleFilterColumns="handleFilterColumns">
            </table-column-filter-dialog>
        </div>
        <kt-table :height="380" :data="pageResult" :columns="filterColumns" @handleCurrentChange="showMenus"
                  @findPage="findPage" @handleEdit="handleEdit" @handleDelete="handleDelete">
        </kt-table>

        <!--角色菜单，表格树内容栏-->
        <div class="menu-container">
            <div class="menu-header">
                <span><B>角色菜单授权</B></span>
            </div>
            <el-table ref="menus" :data="menus" @selection-change="choose">
                <el-table-column type="selection" width="65" v-model="menuIds" prop="id"></el-table-column>
                <el-table-column type="index" label="序号" width="290"></el-table-column>
                <el-table-column property="title" label="菜单名" width="350"></el-table-column>
                <el-table-column property="createTime" label="创建时间" width="350"></el-table-column>
            </el-table>
            <div style="margin-top: 20px;">
                <el-button @click="submitSelect">提交</el-button>
                <el-button @click="clearSelect">取消</el-button>
            </div>
        </div>
        <!--新增编辑界面-->
        <el-dialog :title="operation?'新增':'编辑'" width="40%" :visible.sync="dialogVisible" :close-on-click-modal="false">
            <el-form :model="dataForm" label-width="80px" :rules="dataRule" ref="dataForm" :size="size"
                     label-position="right">
                <el-form-item label="ID" prop="id" v-if="false">
                    <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="角色名" prop="name">
                    <el-input v-model="dataForm.name" auto-complete="off"
                              :disabled="dataForm.disableUserName"></el-input>
                </el-form-item>
                <el-form-item label="备注" prop="description">
                    <el-input v-model="dataForm.description" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="角色" v-if="!operation">
                    <el-select v-model="role" placeholder="请选择"
                               style="width: 100%;">
                        <el-option v-for="item in userRoles" :key="item.id"
                                   :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :size="size" @click.native="dialogVisible = false">{{$t('action.cancel')}}</el-button>
                <el-button :size="size" type="primary" @click.native="submitForm" :loading="editLoading">
                    {{$t('action.submit')}}
                </el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
  import PopupTreeInput from '@/components/PopupTreeInput'
  import KtTable from '@/components/Core/KtTable'
  import KtButton from '@/components/Core/KtButton'
  import TableColumnFilterDialog from '@/components/Core/TableColumnFilterDialog'

  export default {
    components: {
      PopupTreeInput,
      KtTable,
      KtButton,
      TableColumnFilterDialog
    },
    data () {
      return {
        filters: {
          name: '',
          authorName: ''
        },
        pageRequest: {pageNum: 1, pageSize: 15},
        pageResult: {},
        columns: [],
        filterColumns: [],
        size: 'small',
        operation: false, // true:新增, false:编辑
        dialogVisible: false,
        editLoading: false,
        dataForm: {
          disableUserName: false,
          id: 0,
          name: '',
          description: '',
          status: 1,
          unique: ''
        },
        userRoles: [],
        role: '',
        dataRule: {
          name: [{required: true, message: '名称不能为空', trigger: 'blur'}]
        },
        menus: [],
        menuIds: [],
        menusColumn: {},
        currentRoleId: 0
      }
    },
    methods: {
      findPage: function (data) {
        if (data != null) {
          this.pageRequest = data.pageRequest
        }
        this.pageRequest.name = this.filters.name
        this.pageRequest.status = 1
        this.$api.Role.findPage(this.pageRequest).then((res) => {
          this.pageResult = res.body
          this.showMenus()
        }).then(data != null ? data.callback : '')
      },
      // 处理表格列过滤显示
      initColumns: function () {
        this.columns = [
          {prop: 'id', label: 'ID', minWidth: 50},
          {prop: 'name', label: '角色名', minWidth: 50},
          {prop: 'description', label: '备注', minWidth: 60},
          {prop: 'parentId', label: '父级', minWidth: 60},
          {prop: 'createTime', label: '创建时间', minWidth: 50},
          {prop: 'unique', label: '唯一码', minWidth: 50},
          {
            prop: 'status', label: '状态', minWidth: 40, formatter: function (row) {
              if (row.status === 1) {
                return '正常'
              } else if (row.status === 2) {
                return '异常'
              } else if (row.status === 3) {
                return '删除'
              }
            }
          }
        ]
        this.filterColumns = this.columns
      },
      handleEdit: function (data) {
        this.dialogVisible = true
        this.operation = false
        this.dataForm = {
          disableUserName: true,
          id: data.row.id,
          name: data.row.name,
          description: data.row.description
        }
      },
      handleAdd: function (data) {
        this.dialogVisible = true
        this.operation = true
        this.dataForm = {
          disableUserName: false,
          id: 0,
          userName: '',
          password: '',
          email: '',
          mobilePhone: '',
          status: 1
        }
      },
      handleDelete: function (data) {
        this.dataForm = {
          unique: data.params.unique
        }
        this.$api.Role.delRole(this.dataForm).then(data != null ? data.callback : '')
      },
      submitForm: function () {
        this.$api.Role.saveRole(this.dataForm).then((res) => {
          if (res.isSuccess == true) {
            this.dialogVisible = false
            this.findPage()
            this.$message.success('保存成功')
          } else {
            this.$message.error(res.msg)
          }
        }).catch((res) => {
          this.$message.error(res.msg)
        })
      },
      // 处理表格列过滤显示
      handleFilterColumns: function (data) {
        this.filterColumns = data.filterColumns
        this.$refs.tableColumnFilterDialog.setDialogVisible(false)
      },
      // 处理表格列过滤显示
      displayFilterColumnsDialog: function () {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true)
      },
      clearSelect: function () {
        this.$refs.menus.clearSelection()
      },
      submitSelect: function () {
        let ids = ''
        this.menuIds.forEach((row) => {
          ids += row.id + ','
        })
        this.$api.Role.saveRoleMenus({'roleId': this.currentRoleId, 'ids': ids}).then((res) => {
          if (res.isSuccess == true) {
            this.$message.success('保存成功')
          } else {
            this.$message.error(res.msg)
          }
        }).catch((res) => {
          this.$message.error(res.msg)
        })
      },
      showMenus: function (row) {
        let id = ''
        if (row && row.val) {
          id = row.val.id
          this.currentRoleId = id
        }
        this.$api.menu.findList({status: 1}).then((res) => {
          if (res.isSuccess === true) {
            this.menus = res.body
            this.$api.Role.findRoleMenu({roleId: id}).then((ress) => {
              if (ress.isSuccess === true) {
                if (ress.body) {
                  ress.body.forEach((row) => {
                    this.menus.forEach((rowtop) => {
                      if (rowtop.id === row.id) {
                        this.$refs.menus.toggleRowSelection(rowtop)
                      }
                    })
                  })
                }
              }
            })
          }
        })
      },
      choose (val) {
        this.menuIds = val
      }
    },
    mounted () {
      this.initColumns()
      this.$api.Role.findPage({}).then((res) => {
        this.userRoles = res.body.content
      })
    }
  }
</script>

<style scoped>
    .page-container {
        height: 770px;
    }

    .menu-container {
        margin-top: 40px;
    }

    .menu-header {
        padding-left: 8px;
        padding-bottom: 5px;
        text-align: left;
        font-size: 16px;
        color: rgb(20, 89, 121);

    }
</style>
